package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.dialog.ShopDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/11/8 0008
 */

public class TerminalEditorActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.confirm_data)
    private TextView mTextViewConfirm;

    private EditText mEditTextShop;
    private EditText mEditTextTerminal;
    private EditText mEditTextCashier;
    private EditText mEditTextPos;
    private EditText mEditTextPrint;

    private String shopId;
    private String shopName;
    private String terminalId;
    private String terminal;
    private String cashier;
    private String posId;
    private String printId;

    private String type;

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        type = intent.getStringExtra("type");
        if ("edit".equals(type)) {
            shopId = intent.getStringExtra("shop_id");
            shopName = intent.getStringExtra("shop_name");
            terminalId = intent.getStringExtra("terminal_id");
            terminal = intent.getStringExtra("terminal_name");
            cashier = intent.getStringExtra("worker_name");
            posId = intent.getStringExtra("pos_name");
            printId = intent.getStringExtra("print_name");
        }
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_terminal_editor);
    }

    @Override
    protected void initViews() {
        mEditTextShop = (EditText) findViewById(R.id.shop_name);
        mEditTextTerminal = (EditText) findViewById(R.id.terminal_name);
        mEditTextCashier = (EditText) findViewById(R.id.cashier_name);
        mEditTextPos = (EditText) findViewById(R.id.pos_number);
        mEditTextPrint = (EditText) findViewById(R.id.print_number);
        if ("add".equals(type)) {
            mViewTitle.setText("新增终端");
            mEditTextShop.setText(null);
            mEditTextShop.setOnClickListener(this);
            mEditTextTerminal.setText(null);
            mEditTextCashier.setText(null);
            mEditTextPos.setText(null);
            mEditTextPrint.setText(null);
        } else if ("edit".equals(type)) {
            mViewTitle.setText("编辑终端");
            mEditTextShop.setText(shopName);
            mEditTextShop.setOnClickListener(null);
            mEditTextTerminal.setText(terminal);
            mEditTextCashier.setText(cashier);
            mEditTextPos.setText(posId);
            mEditTextPrint.setText(printId);
        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mEditTextShop.setOnClickListener(this);
        mEditTextTerminal.setOnClickListener(this);
        mEditTextCashier.setOnClickListener(this);
        mEditTextPos.setOnClickListener(this);
        mEditTextPrint.setOnClickListener(this);
        mTextViewConfirm.setOnClickListener(this);
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.shop_name:
                showShopDialog();
                break;
            case R.id.terminal_name:
                mEditTextTerminal.requestFocus();
                break;
            case R.id.cashier_name:
                mEditTextCashier.requestFocus();
                break;
            case R.id.pos_number:
                mEditTextPos.requestFocus();
                break;
            case R.id.print_number:
                mEditTextPrint.requestFocus();
                break;
            case R.id.confirm_data:
                confrimData();
                break;
        }
    }

    /**
     * 检查提交信息
     */
    private void confrimData() {
        shopName = mEditTextShop.getText().toString();
        terminal = mEditTextTerminal.getText().toString();
        cashier = mEditTextCashier.getText().toString();
        posId = mEditTextPos.getText().toString();
        printId = mEditTextPrint.getText().toString();
        if (CommonUtil.isEmpty(shopId)) {
            showToast("请选择门店");
            return;
        }
        if (CommonUtil.isEmpty(terminal)) {
            showToast("请输入终端名称");
            return;
        }
        if (CommonUtil.isEmpty(cashier)) {
            showToast("请输入收银员ID");
            return;
        }
        if (CommonUtil.isEmpty(posId)) {
            showToast("请输入POS机具编号");
            return;
        }
        if (CommonUtil.isEmpty(printId)) {
            showToast("请输入打印机具编号");
            return;
        }
        if ("add".equals(type)) {
            addTerminal();
        } else if ("edit".equals(type)) {
            editTerminal();
        }

    }

    /**
     * 新增中终端
     */
    private void addTerminal() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.TERMINAL_ADD);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", String.valueOf(PreferenceUtil
                .getParam(Preferences.LOGINTYPE, "")));
        param.add("shop_id", shopId);
        param.add("name", terminal);
        param.add("workmen_uid", cashier);
        param.add("device_pos", posId);
        param.add("device_print", printId);
        param.add("sign", param.getMd5String());
        showLoading("处理中");
        HttpUtils.getInstance().url(ApiName.API).params(param)
                .executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil
                            .GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        showToast(result.getInfo());
                        backTolast("");
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }

    /**
     * 返回上一级
     */
    private void backTolast(String terminal_name) {
        if (CommonUtil.isEmpty(terminal_name)) {
            terminal_name = "";
        }
        Intent intent = new Intent();
        intent.putExtra("terminal_name", terminal_name);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 编辑终端
     */
    private void editTerminal() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.TERMINAL_EDIT);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", String.valueOf(PreferenceUtil
                .getParam(Preferences.LOGINTYPE, "")));
        param.add("terminal_id", terminalId);
        param.add("shop_id", shopId);
        param.add("name", terminal);
        param.add("workmen_uid", cashier);
        param.add("device_pos", posId);
        param.add("device_print", printId);
        param.add("sign", param.getMd5String());
        showLoading("处理中");
        HttpUtils.getInstance().url(ApiName.API).params(param)
                .executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissLoading();
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissLoading();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        showToast(result.getInfo());
                        try {
                            String terminal_name = new JSONObject(string)
                                    .getJSONObject("data")
                                    .optString("terminal_name");
                            backTolast(terminal_name);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }

    private ShopDialog mShopDialog;

    /**
     * 选择门店信息
     */
    private void showShopDialog() {
        if (mShopDialog == null) {
            mShopDialog = new ShopDialog(getThis());
        }
        mShopDialog.setCancelable(true);
        mShopDialog.setShopDialogListener(new ShopDialog.ShopDialogListener() {
            @Override
            public void onItemSelected(String name, String id) {
                super.onItemSelected(name, id);
                mEditTextShop.setText(name);
                shopId = id;
            }
        });
        mShopDialog.show();
    }

}
